package com.cloudsea.yunhaianalysis.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.cloudsea.yunhaianalysis.dto.ReportQueryDTO;
import com.cloudsea.yunhaianalysis.mapper.AnalysisMapper;
import com.cloudsea.yunhaianalysis.service.ReportService;
import com.cloudsea.yunhaianalysis.vo.BusinessHoursVO;
import com.cloudsea.yunhaianalysis.vo.SalesTrendVO;
import com.cloudsea.yunhaianalysis.vo.TopSellingItemVO;
import com.cloudsea.yunhaistores.entity.MerchantUsers;
import com.cloudsea.yunhaistores.entity.Stores;
import com.cloudsea.yunhaistores.mapper.MerchantUsersMapper;
import com.cloudsea.yunhaistores.mapper.StoresMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private AnalysisMapper analysisMapper;

    @Autowired
    private MerchantUsersMapper merchantUsersMapper;

    @Autowired
    private StoresMapper storesMapper;

    @Override
    public List<TopSellingItemVO> getTopSellingReport(ReportQueryDTO dto) {
        this.checkStorePermission(dto.getStoreId()); // <<<--- 修改点

        LocalDateTime startTime = dto.getStartDate().atStartOfDay();
        LocalDateTime endTime = dto.getEndDate().atTime(LocalTime.MAX);

        return analysisMapper.getTopSellingItems(dto.getStoreId(), startTime, endTime, dto.getTopN());
    }

    @Override
    public List<SalesTrendVO> getSalesTrendReport(ReportQueryDTO dto) {
        // 【权限校验】复用已有的安全逻辑
        this.checkStorePermission(dto.getStoreId());

        LocalDateTime startTime = dto.getStartDate().atStartOfDay();
        LocalDateTime endTime = dto.getEndDate().atTime(LocalTime.MAX);

        return analysisMapper.getSalesTrend(dto.getStoreId(), startTime, endTime);
    }

    @Override
    public List<BusinessHoursVO> getBusinessHoursReport(ReportQueryDTO dto) {
        // 复用已有的权限校验逻辑
        this.checkStorePermission(dto.getStoreId());

        LocalDateTime startTime = dto.getStartDate().atStartOfDay();
        LocalDateTime endTime = dto.getEndDate().atTime(LocalTime.MAX);

        return analysisMapper.getBusinessHours(dto.getStoreId(), startTime, endTime);
    }


    /**
     * 将重复的权限校验逻辑提取到一个私有方法中
     */
    private void checkStorePermission(Long storeId) {
        long loginId = StpUtil.getLoginIdAsLong();
        MerchantUsers currentUser = merchantUsersMapper.selectById(loginId);
        if (currentUser == null || currentUser.getMerchantId() == null) {
            throw new SecurityException("无法获取当前用户信息，权限校验失败");
        }
        Long operatorMerchantId = currentUser.getMerchantId();

        Stores targetStore = storesMapper.selectById(storeId);
        if (targetStore == null) {
            throw new RuntimeException("查询的门店不存在");
        }
        Long targetMerchantId = targetStore.getMerchantId();

        if (!operatorMerchantId.equals(targetMerchantId)) {
            throw new SecurityException("权限不足，您只能查询自己商户下的门店数据");
        }
    }
}
